package com.jadcon.bedbreakfast.domain;

import java.util.Date;

import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class ReservationValidator implements Validator {

	    @SuppressWarnings("unchecked")
		public boolean supports(Class clazz) {
	        return Reservation.class.equals(clazz);
	    }
	    
	    public void validate(Object object, Errors e) {
	        Reservation reservation = (Reservation) object;
	        Date checkIn = reservation.getCheckIn();
	        Date checkOut = reservation.getCheckOut();

	        if (checkIn.after(checkOut)) {
	        	e.rejectValue("checkIn", "checkIn.Invalid");
	        }
	        else if (checkIn.equals(checkOut)){
	        	e.rejectValue("checkIn", "checkIn.Invalid");
	        }
	        else if (checkOut.before(checkIn)) {
	        	e.rejectValue("checkIn", "checkIn.Invalid");
	        }
	    }
}
